package com.ql.dao;

import com.ql.domain.Device;
import com.ql.domain.Section;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.List;

public interface DeviceDao {
//    @Select("select * from device")

    /**
     * 条件查询所有
     *
     * @param sectionId
     * @return
     */
 /*   @Select("<script>" +
            "select * from device " +
            "<where>" +
            "<if test=\"sectionId!=null and sectionId!=''\">" +
            " and section_id = #{sectionId}" + "</if>" +
            "<if test=\"devNum!=null and devNum!=''\">" +
            " and devNum = #{devNum}" +
            "</if>" +
            "</where>" +
            "order by orderBy ASC" +
            "</script>")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "devName", property = "devName"),
            @Result(column = "devNum", property = "devNum"),
            @Result(column = "machineNum", property = "machineNum"),
            @Result(column = "devCode", property = "devCode"),
            @Result(column = "manufacturers", property = "manufacturers"),
            @Result(column = "orderBy", property = "orderBy"),
            @Result(column = "state", property = "state"),
            @Result(column = "devTotal", property = "devTotal"),
            @Result(column = "section_id", property = "section_id"),
            @Result(column = "section_id", property = "section",
                    javaType = Section.class,
                    one = @One(select = "com.ql.dao.SectionDao.findByDeviceId"))
    })
    List<Device> findAll(@Param("sectionId") String sectionId, @Param("devNum") String devNum);
*/
    /**
     * 添加设备
     *
     * @param device
     */
    @Insert("insert into device values(#{id},#{devName},#{devNum},#{machineNum},#{devCode},#{manufacturers},#{orderBy},#{state},#{devTotal},#{section_id})")
    void save(Device device);

    /**
     * 修改设备信息
     *
     * @param device
     */
    void update(Device device);

    /**
     * 根据sectionId查询出标段关联的设备
     *
     * @param sectionId
     * @return
     */
    @Select("select * from device where section_id =#{sectionId}")
    List<Device> findDeviceIdsBySectionId(String sectionId);

    @Select("<script>" +
            "  select * from device" +
            " <where>" +
            "id in" +
            "<foreach collection=\"list\" open=\"(\" separator=\",\" close=\")\" item=\"deviceId\">" +
            "#{deviceId}" +
            "</foreach>" +
            "        <if test=\"devNum !=null and devNum !='' \">" +
            "                and devNum =#{devNum}" +
            "        </if>" +
            "</where>" +
            "order by orderBy ASC" +
            "</script>"
    )
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "devName", property = "devName"),
            @Result(column = "devNum", property = "devNum"),
            @Result(column = "machineNum", property = "machineNum"),
            @Result(column = "devCode", property = "devCode"),
            @Result(column = "manufacturers", property = "manufacturers"),
            @Result(column = "orderBy", property = "orderBy"),
            @Result(column = "state", property = "state"),
            @Result(column = "devTotal", property = "devTotal"),
            @Result(column = "section_id", property = "section_id"),
            @Result(column = "section_id", property = "section",
                    javaType = Section.class,
                    one = @One(select = "com.ql.dao.SectionDao.findByDeviceId"))
    })
    List<Device> findAllByCondition(@Param("list") ArrayList<Integer> deviceIds, @Param("devNum") String devNum);

    /**
     * 删除设备
     * @param id
     */
    @Delete("delete from device where id=#{id}")
    void del(String id);
}
